//
//  TF_AnnounceMentView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/16.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TF_AnnounceMentView: TFtouchesMoved {

    var glView:UIView!
    var ImgView:UIImageView!
    var contentView:UIScrollView!
    var contentLabel:UILabel!
    var close:UIButton!

    //是否有内容
    var iscontent:Bool!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.glView = UIView.init()
        addSubview(self.glView)
        
        
        self.ImgView = UIImageView.init()
        self.glView.addSubview(self.ImgView)
        self.ImgView.backgroundColor = UIColor.clear
        self.ImgView.image = UIImage.init(named: "HTLive.bundle/bulletin_2")
        
        
        self.contentView = UIScrollView.init()
        self.contentView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
    
        self.contentView.isScrollEnabled = true;
        self.glView.addSubview(self.contentView)
       
        self.contentLabel = UILabel.init()
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = UIFont.PingFang(size: 16)
   
        self.contentLabel.text = "暂无公告";
        self.contentLabel.textAlignment = .center
        self.contentLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
        self.contentView.addSubview(self.contentLabel)
    
        self.close = UIButton.init()
        self.close.setImage(UIImage.init(named: "HTLive.bundle/close_white"), for: .normal)
        self.glView.addSubview(self.close)
        self.close.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
       
    }
    
   @objc func closeClick() {
       self.alpha = 0;
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.glView.frame = self.bounds;
        self.ImgView.frame = self.glView.bounds;
        self.contentView.frame = CGRect(x: 12, y: 56, width: self.glView.frame.size.width-24, height: self.frame.size.height-56-12)
        self.close.frame = CGRect(x: self.glView.frame.size.width - 32 - 10, y: 20, width: 32, height: 32)
        if (self.iscontent == false) {
            self.contentLabel.text = "暂无公告";
            self.contentLabel.frame = CGRect(x: 20, y: 10, width: self.contentView.frame.size.width-40, height: self.contentView.frame.size.height-20)
        }
    }
    
    @objc public func setAnnouncement(_ result:TFAnnouncement)  {
        self.iscontent = true;
        if (result.content.count > 0) {
            self.contentLabel.text = result.content;
            let rect = TalkfunSharedTools.getRectWith(result.content, size: CGSize(width: self.glView.frame.size.width-64, height: CGFloat(MAXFLOAT)), fontSize: 16)
            if (rect.size.height > 94) {
                self.contentLabel.frame = CGRect(x: 20, y: 10, width: self.contentView.frame.size.width-40, height: rect.height)
               
            } else {
                self.contentLabel.frame = CGRect(x: 20, y: 10, width: self.contentView.frame.size.width-40, height: self.contentView.frame.size.height-20)
           
               
            }
            
            if (rect.size.height > 20) {
                self.contentLabel.textAlignment = .left
            } else {
                self.contentLabel.textAlignment = .center
            }
          
            self.contentView.contentSize = CGSize(width: 0, height: rect.size.height+30)
        } else {
            self.contentLabel.text = "暂无公告";
            self.contentLabel.frame = CGRect(x: 20, y: 10, width: self.contentView.frame.size.width-40, height: self.contentView.frame.size.height-20)
       
            self.contentLabel.textAlignment = .center
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}
